<?php
global $AppUI, $m, $tab;
require_once($AppUI->getModuleClass('quotations'));
// Get login object
$uid = $AppUI->user_id;
// Check permission
// TODO: Check permission role
// Check edit
$perms =& $AppUI->acl();
$canAccess = $perms->checkModuleItem('quotations', 'access');
$canEdit = $perms->checkModuleItem('quotations', 'edit');
$canDelete = $perms->checkModuleItem('quotations', 'delete');

// check permission list quotations
if(!$canAccess){
	$AppUI->redirect(PUBLIC_PAGE);
}

/*
 * Business logic
 *
 */
//$page_offset = intval(dPgetParam($_GET, 'page_offset', 0));
//$page_limit = intval(dPgetParam($_POST, 'page_limit', 10));
// Condition search
$extra = array('where' => array());
$keyword = dPgetParam($_POST, 'quote_code', '');
if($keyword != ''){
	$extra['where'][] .= " quote_code like '%". $keyword . "%'";
}

$quote_status = intval( dPgetParam( $_POST, 'quote_status', 0 ) );
if ($quote_status > 0) {
	$extra['where'][] .= " quote_status = $quote_status";
}

$quote_customer = intval( dPgetParam( $_POST, 'quote_customer', 0 ) );
if ($quote_customer > 0) {
	$extra['where'][] .= " quote_customer = $quote_customer";
}

$quote_start_date = dPgetParam($_POST, 'quote_start_date', '');
if ($quote_start_date != '') {
	$extra['where'][] .= " quote_start_date >= '$quote_start_date'";
}

$quote_expire_date = dPgetParam($_POST, 'quote_expire_date', '');
if ($quote_expire_date != '') {
	$extra['where'][] .= " quote_expire_date <= '$quote_expire_date'";
}

$extra['where'] = implode(' AND ', $extra['where']);

$obj = new CQuotations();
$result = $obj->getListQuotations($uid, null, $extra);
//$final = array();
//
//foreach($result as $item){
//	$final[$item[$prefix.'customer']]['name'] = $item['cus_name'];
//	$final[$item[$prefix.'customer']]['id'] = $item[$prefix.'customer'];
//	$final[$item[$prefix.'customer']]['item_list'][] =
//		array('id' => $item[$prefix.'id'], 'code' => $item[$prefix.'code'],
//			 //'name' => $item[$prefix.'name'], 'status' => $item[$prefix.'status'],
//			 'name' => $item[$prefix.'name'], 'status' => $item['status_name'], 'status_id' => $item[$prefix.'status'],
//			 //'type' => $item[$prefix.'type'], 'shipping_type' => $item[$prefix.'shipping_type']);
//			 'type' => $item['type_name'], 'shipping_type' => $item['shipping_type_name'],
//			 'completed' => $item['quote_completed']);
//
//	if($act_status_id==intval($item[$prefix.'status'])){
//		$confNum += 1;
//	}
//}

header('Content-Type: text/xml');

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('root');

obj2xml($xml, 'quotations', 'item', $result, 'quote_id', 'quote_code');

$xml->endElement();
echo $xml->outputMemory(true);
?>